import {
  detectorLedgerMap,
  acidDrumLedgerMap,
  bottleLedgerCheckListMap,
  bottleLedgerBottleInfoMap,
  bottleLedgerSupplyBottleMap,
  bottleLedgerReturnBottleMap,
  bottleLedgerExpiredBottleMap
} from "./utils";

/**
 * @description: 格式化单个JSON数据
 * @param {string} type
 * @param {string} key
 * @param {Map} modelMap
 * @param {*} string
 * @param {Record} data
 * @param {*} any
 * @return {*}
 */
export const formatJSONDataItem = (type: string, key: string, modelMap: Map<string, string>, data: Record<string, any>) => {
  const label = modelMap.get(key);
  if (
    (type === "侦测器台账" && key === "fileNameList") ||
    (type === "工单作业SOP管理" && key === "gasNameList") ||
    (type === "料号与气体映射" && key === "fileNameList")
  ) {
    return {
      label,
      value: data[key].length ? data[key].join("、") : "--"
    };
  } else {
    return {
      label,
      value: data[key] || "--"
    };
  }
};

/**
 * @description: 格式化JSON数据
 * @param {string} type
 * @param {string} data
 * @return {*}
 */
export const useFormatJSON2Array = (type: string, data: string) => {
  if (!data) return [];
  const realData = JSON.parse(data);
  return Object.keys(realData)
    ?.map(key => {
      switch (type) {
        case "侦测器台账":
          return formatJSONDataItem(type, key, detectorLedgerMap, realData);
        case "酸桶台账":
          return formatJSONDataItem(type, key, acidDrumLedgerMap, realData);
        case "工单作业SOP管理":
          return formatJSONDataItem(type, key, bottleLedgerCheckListMap, realData);
        case "料号与气体映射":
          return formatJSONDataItem(type, key, bottleLedgerBottleInfoMap, realData);
        case "气瓶台账-在供":
          return formatJSONDataItem(type, key, bottleLedgerSupplyBottleMap, realData);
        case "气瓶台账-返库":
          return formatJSONDataItem(type, key, bottleLedgerReturnBottleMap, realData);
        case "气瓶台账-废料":
          return formatJSONDataItem(type, key, bottleLedgerExpiredBottleMap, realData);
      }
    })
    ?.filter(item => {
      return !!item?.label;
    });
};
